#pragma once

#include "loirey_GLOBAL.h"
#include "loirey_MyMath.h"
#include "loirey_BasicStructure.h"

namespace loirey
{
	template <typename TState, typename TSolution>
	class CMemorialSearcher
	{
	public:
		typedef map<TState, TSolution> TMap;

	public:
		TMap Memory;

	protected:
		virtual bool _LeafSolve(const TState& State, TSolution& Solution)
		{
			return false;
		}
		virtual void _Search(const TState& State, TSolution& Solution) = 0;
	public:
		CMemorialSearcher()
		{
			ClearMemory();
		}
		void ClearMemory()
		{
			Memory.clear();
		}
		virtual void Search(const TState& State, TSolution& Solution)
		{
			TMap::const_iterator itr = Memory.find(State);
			if (itr == Memory.end())
			{
				if (!_LeafSolve(State, Solution))
					_Search(State, Solution);
				Memory[State] = Solution;
			}
			else
				Solution = itr->second;
		}
	};
}

